package com.leyou.user.controller;


import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
//@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名或手机号来查询
     * 查到false
     * 没查到true
     *
     * @param data
     * @param type
     * @return
     */
    @GetMapping("check/{data}/{type}")
    @ApiOperation(value = "校验用户名数据是否可用，如果不存在则可用")
    @ApiResponses({
            @ApiResponse(code = 200, message = "校验结果有效，true或false代表可用或不可用"),
            @ApiResponse(code = 400, message = "请求参数有误，比如type不是指定值")
    })
    public ResponseEntity<Boolean> check(
            @ApiParam(value = "要校验的数据", example = "lisi")@PathVariable("data") String data,
            @ApiParam(value = "数据类型，1：用户名，2：手机号", example = "1") @PathVariable("type") Integer type

    ) {
        return ResponseEntity.ok(this.userService.check(data, type));
    }

    /**
     * 发送短信验证码到mp
     * @param phone
     * @return
     */
    @PostMapping("code")
    public ResponseEntity<Void> sendVerifyCode(@RequestParam("phone") String phone) {
        this.userService.sendVerifyCode(phone);
//        相应成功状态码204
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

//    服务端校验，目前方式Hibernate validator，首先把注解加到pojo上，在请求方法中加注解，让服务端校验生效
    @PostMapping("register")

    public ResponseEntity<Void> register(
            @Valid User user, @RequestParam("code") String code){
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名和密码查询用户
     * @param username 用户名
     * @param password 密码
     * @return 用户信息
     */
    @GetMapping("query")
    public ResponseEntity<UserDTO> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password){
        return ResponseEntity.ok(userService.login(username, password));
    }

}
